import collections
from functools import cmp_to_key
class Solution(object):
    def evolutionaryRecord(self, parents):
        d = collections.defaultdict(set)
        def cp(x, y):
            s_x = x[0]
            s_y = y[0]
            m_x = len(s_x)
            m_y = len(s_y)
            if len(s_x) == len(s_y):
                if s_x <= s_y:
                    return -1
                else:
                    return 1
            else:
                if m_x < m_y:
                    if s_x < s_y[:m_x]:
                        return -1
                    else:
                        return 1
                else:
                    if s_x[:m_y] <= s_y:
                        return -1
                    else:
                        return 1
                    

        for r, v in enumerate(parents):
            if v != -1:
                d[v].add(r)
            else:
                start = r
        def dfs(node):
            if node == start:
                pre = ''
                c = 0
            else:
                pre = '0'
                c = 1
            if not d[node]:
                return pre, c
            else:
                next_s = []
                for v in d[node]:
                    next_s.append(dfs(v))
                if len(next_s) == 1:
                    return pre + next_s[0][0], next_s[0][1] + c
                else:
                    next_s.sort(key=cmp_to_key(cp))
                now_s = []
                for i in range(len(next_s) - 1):
                    now_s.append(next_s[i][0])
                    now_s.append(next_s[i][1] * '1')
                now_s.append(next_s[-1][0])
                return pre + ''.join(now_s), next_s[-1][1] + c
        return dfs(start)[0]





data = Solution()
parents = [-1,0,0,1,2,2]
print(data.evolutionaryRecord(parents))
# parents = [-1,0,0,2]
# print(data.evolutionaryRecord(parents))